
Imports System
Imports System.Collections
Imports System.Data.SqlClient
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' List of a user's favorite users.
	''' </summary>
	Public Class UsersFavorites
		Inherits UserUsersList
		Public Sub New(ByVal usr As User)
			MyBase.New(usr)
		End Sub

		Public Sub New(ByVal userID As Integer)
			MyBase.New(userID)
		End Sub

		Protected Overloads Overrides Function LoadList(ByVal userID As Integer) As Hashtable
			Dim favList As New Hashtable()

			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand(GetSelectSQL())
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				Dim dr As SqlDataReader = cmd.ExecuteReader()
				While dr.Read()
					Dim fav As New FavoriteUser(dr.GetInt32(dr.GetOrdinal("ID")))
					favList.Add(fav.GetHashCode(), fav)
				End While
				dr.Close()
			Finally
				conn.Close()
			End Try

			Return favList
		End Function

		Protected Overloads Overrides Sub ClearList()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("DELETE FROM FavoriteUsers WHERE UID=@UID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal item As Object)
			Dim fav As FavoriteUser = DirectCast(item, FavoriteUser)
			DeleteItem(fav.ID)
		End Sub

		Protected Overloads Overrides Sub DeleteItem(ByVal favUID As Integer)
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("DELETE FROM FavoriteUsers WHERE UID=@UID AND FavoriteUID=@FavoriteUID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)
				cmd.Parameters.Add("@FavoriteUID", favUID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Overloads Overrides Function AddItem(ByVal item As Object) As Boolean
			Dim fav As FavoriteUser = DirectCast(item, FavoriteUser)
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()
			Try
				Dim cmd As New SqlCommand("INSERT INTO FavoriteUsers (UID, FavoriteUID) VALUES (@UID, @FavoriteUID)")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", userID)
				cmd.Parameters.Add("@FavoriteUID", fav.ID)

				cmd.ExecuteNonQuery()
			Finally
				conn.Close()
			End Try

			Return True
		End Function

		Protected Function GetSelectSQL() As String
			Dim sql As String = "SELECT usr.* FROM" + Environment.NewLine
			sql += "" & Chr(9) & "FavoriteUsers fav," + Environment.NewLine
			sql += "" & Chr(9) & "Users usr" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "fav.UID=@UID" + Environment.NewLine
			sql += "AND" & Chr(9) & "fav.FavoriteUID = usr.ID" + Environment.NewLine
			Return sql
		End Function
	End Class
End Namespace






